<template>
  <div class="date-wrap">
    <el-icon><Calendar /></el-icon
    ><Datepicker
      v-model="searchDate"
      :locale="zhCN"
      @closed="pickerClose"
      ref="datePickerRef"
    />
  </div>
</template>
<script lang="ts" setup>
import Datepicker from "vue3-datepicker";
import { ref, onMounted } from "vue";
import { Calendar } from "@element-plus/icons-vue";
import { ElIcon } from "element-plus";
import { zhCN } from "date-fns/locale";
import dayjs from "dayjs";

const searchDate = ref(new Date());
const datePickerRef = ref(null);
const emits = defineEmits(["change"]);
const preDateStr = ref("");
function pickerClose() {
  if (datePickerRef.value) {
    datePickerRef.value.viewShown = "none";
    // const dateStr = datePickerRef.value.input;
    // console.log("dateStr:", dateStr);
  }

  // console.log("datePickerRef:", datePickerRef.value);
  // console.log("datePickerRef.value.value:", datePickerRef.value.value);
  // const dateStr = dayjs(new Date(searchDate.value)).format("YYYY-MM-DD");
  // console.log("dateStr2:", dateStr);
  setTimeout(() => {
    const dateStr = dayjs(new Date(searchDate.value)).format("YYYY-MM-DD");
    if (preDateStr.value !== dateStr) {
      preDateStr.value = dateStr;
      console.log("dateStr3:", dateStr);
      emits("change", dateStr);
    }
  }, 0);
}

onMounted(() => {
  const originFocus = datePickerRef.value.focus;
  datePickerRef.value.focus = () => {};
  datePickerRef.value.click = originFocus;
});
</script>
<style lang="scss" scoped>
.date-wrap {
  width: 200px;
  height: 50px;
  color: #3f4254;
  background: #f9f9f9;
  display: flex;
  align-items: center;
  border-radius: 4px;
  padding: 1px 11px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  transform: translateZ(0);
  transition: box-shadow 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  box-sizing: border-box;
  vertical-align: middle;
  z-index: 100;
  position: relative;
  &:hover {
    // border-color: #3689f5;
    box-shadow: 0 0 0 1px #3689f5 inset;
  }

  :deep(.v3dp__datepicker) {
    margin-left: 10px;
    background: transparent;
    width: calc(100% - 30px);
    height: 50px;
    line-height: 50px;
    .v3dp__input_wrapper {
      input {
        width: 100%;
        background: transparent;
      }
    }
    .v3dp__popout {
      // width: 24em;
      font-size: 26px !important;
      left: -40px;
    }
  }
}
</style>
